package org.northking.controller;

import org.northking.pojo.Apple;
import org.northking.pojo.Mango;
import org.northking.pojo.Strawberry;
import org.northking.service.ConsumerA;
import org.northking.service.ConsumerB;
import org.northking.service.ConsumerC;
import org.northking.service.ConsumerD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
public class ConsumerShopping {

    @Autowired
    private ConsumerA consumerA;
    @Autowired
    private ConsumerB consumerB;
    @Autowired
    private ConsumerC consumerC;
    @Autowired
    private ConsumerD consumerD;

    @Autowired
    private Apple apple;
    @Autowired
    private Strawberry strawberry;
    @Autowired
    private Mango mango;

    @PostConstruct
    private void init() {
        apple.setPrice(8);
        strawberry.setPrice(13);
        mango.setPrice(20);
    }
    @GetMapping("/consumerA")
    public double ConsumerA(int appleCount, int strawberryCount) {
        Assert.isTrue(appleCount == 8 && strawberryCount == 8, "苹果和草莓的数量都要配置为8");
        double result = (double)(Math.round(consumerA.totalPrice(appleCount,strawberryCount) * 100)) / 100;
        Assert.isTrue(result == 168,"结果不是168，错误");
        return result;
    }

    @GetMapping("/consumerB")
    public double ConsumerB(int appleCount, int strawberryCount, int mangoCount) {
        Assert.isTrue(appleCount == 8 && strawberryCount == 8 && mangoCount == 8, "苹果，草莓，芒果的数量都要配置为8");
        double result = (double)(Math.round(consumerB.totalPrice(appleCount,strawberryCount,mangoCount) * 100)) / 100;
        Assert.isTrue(result == 328,"结果不是328，错误");
        return result;
    }

    @GetMapping("/consumerC")
    public double ConsumerC(int appleCount, int strawberryCount, int mangoCount) {
        Assert.isTrue(appleCount == 8 && strawberryCount == 8 && mangoCount == 8, "苹果，草莓，芒果的数量都要配置为8");
        double result = (double) (Math.round(consumerC.totalPrice(appleCount, strawberryCount, mangoCount) * 100)) / 100;
        Assert.isTrue(result == 307.2,"结果不是307.2，错误");
        return result;
    }

    @GetMapping("/consumerD")
    public double ConsumerD(int appleCount, int strawberryCount, int mangoCount) {
        Assert.isTrue(appleCount == 8 && strawberryCount == 8 && mangoCount == 8, "苹果，草莓，芒果的数量都要配置为8");
        double price = (double) (Math.round(consumerD.totalPrice(appleCount, strawberryCount, mangoCount) * 100))/100;
        if (price >= 100) {
            double result = (double) (Double.valueOf(price * 100).longValue() - 10_00) / 100;
            Assert.isTrue(result == 297.2, "结果不是297.2，错误");
            return result;
        }
        return  price;
    }


}
